document.querySelector('.username').innerHTML = localStorage.getItem('uname')
//获取学生数据
async function getstu() {
    try {
        // let token = localStorage.getItem('token')
        let getmsg = await axios({
            url: '/dashboard'
            // headers: { Authorization: token } 
        })
        console.log(getmsg)
        setmsg(getmsg.data.data.overview)
        let year = getmsg.data.data.year
        setcharts(year)
        let salaryData = getmsg.data.data.salaryData
        // console.log(salaryData);
        setdistribution(salaryData)
        let page = 1
        let groupData = getmsg.data.data.groupData[page]
        setgroup(groupData)
        document.querySelector('#btns').addEventListener('click', function (e) {
            e.preventDefault()
            console.log(e.target.tarName)
            // if(e.target.tagName === '')
            document.querySelector('.btn-blue').classList.remove('btn-blue')
            e.target.classList.add('btn-blue')
            page = e.target.innerText
            console.log(page)
            groupData = getmsg.data.data.groupData[page]
            // console.log(groupData)
            setgroup(groupData)
        })
        setgender(salaryData)
        let provinceData = getmsg.data.data.provinceData
        setorigin(provinceData)
    } catch (err) {
        // pxmu.toast('登录信息过期，请重新登陆')
        // setTimeout(() => {
        //     location.href = './login.html'
        // }, 1000)
    }
}
getstu()
//渲染
function setmsg(msg) {
    for (key in msg) {
        document.querySelector(`.${key}`).innerHTML = msg[key]
    }
    // document.querySelector('.salary').innerHTML = msg.salary
    // document.querySelector('.student_count').innerHTML = msg.student_count
    // document.querySelector('.age').innerHTML = msg.age
    // document.querySelector('.group_count').innerHTML = msg.group_count
}
//退出登录
function logout() {
    document.querySelector('#logout').addEventListener('click', async function () {
        let confirm = await pxmu.diaglog('您确定退出吗?')
        if (confirm.btn == 'right') {
            localStorage.removeItem('uname')
            localStorage.removeItem('token')
            location.href = './login.html'
        }
    })
}
logout()

//2022全学科薪资走势
function setcharts(year) {
    let myBox = echarts.init(document.querySelector('#line'))
    option = {
        title: {
            text: '2022全学科薪资走势',
            textStyle: {
                fontSize: 14
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: year.map(item => item.month)
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: year.map(item => item.salary),
                type: 'line',
                smooth: true,
                symbolSize: 8,
                lineStyle: {
                    width: 6,
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                        colorStops: [{
                            offset: 0, color: '#4b9dea' // 0% 处的颜色
                        }, {
                            offset: 1, color: '#5d7ef5' // 100% 处的颜色
                        }],
                        global: false // 缺省为 false
                    }
                },
                areaStyle: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0, color: '#89bbff' // 0% 处的颜色
                        }, {
                            offset: 1, color: 'rgba(255,255,255,0)' // 100% 处的颜色
                        }],
                        global: false // 缺省为 false
                    }
                }
            }
        ]
    }
    myBox.setOption(option)
}

//班级薪资分布
function setdistribution(salaryData) {
    let myBox = echarts.init(document.querySelector('#salary'))
    option = {
        title: {
            text: '班级薪资分布',
            textStyle: {
                fontSize: 16
            },
            top: '5%',
            left: '5%'
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            bottom: '5%',
            left: 'center'
        },
        series: [
            {
                name: 'Access From',
                type: 'pie',
                radius: ['60%', '70%'],
                avoidLabelOverlap: false,
                itemStyle: {
                    borderRadius: '50%',
                    borderColor: '#fff',
                    borderWidth: 3
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 18,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: salaryData.map(item => {
                    return {
                        value: item.g_count + item.b_count,
                        name: item.label
                    }
                })
            }
        ],
        color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a']
    }
    myBox.setOption(option)
}

//班级每组期望薪资
function setgroup(groupData) {
    let myBox = echarts.init(document.querySelector('#lines'))
    option = {
        tooltip: {},
        dataset: {
            dimensions: ['product', '期望薪资', '就业薪资'],
            source: groupData.map(item => {
                return {
                    product: item.name,
                    期望薪资: item.hope_salary,
                    就业薪资: item.salary
                }
            })
        },
        xAxis: { type: 'category' },
        yAxis: {
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        // Declare several bar series, each will be mapped
        // to a column of dataset.source by default.
        series: [{
            type: 'bar', color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#41cf9e' // 0% 处的颜色
                }, {
                    offset: 1, color: '#daede6' // 100% 处的颜色
                }],
                global: false // 缺省为 false
            }
        },
        {
            type: 'bar', color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#4b9eed' // 0% 处的颜色
                }, {
                    offset: 1, color: '#e2ebf2' // 100% 处的颜色
                }],
                global: false // 缺省为 false
            }
        }]
    };
    myBox.setOption(option)
}

//男女薪资分布
function setgender(salaryData) {
    let myBox = echarts.init(document.querySelector('#gender'))
    // console.log(document.querySelector('#gender'))
    option = {
        // legend: {},
        title: [
            {
                text: '男女薪资分布',
                left: '5%',
                top: '5%',
            },
            {
                text: '男生',
                textStyle: {
                    fontSize: 12
                },
                left: '50%',
                top: '45%',
                textAlign: 'center'
            },
            {
                text: '女生',
                textStyle: {
                    fontSize: 12
                },
                left: '50%',
                top: '85%',
                textAlign: 'center'
            },
        ],
        // tooltip: {},40
        dataset: {
            dimensions: ['product', '男生', '女生'],
            // source: [
            //     { product: 'Milk Tea', 男生: 86.5, 女生: 92.1 },
            //     { product: 'Matcha Latte', 男生: 41.1, 女生: 30.4 },
            //     { product: 'Cheese Cocoa', 男生: 24.1, 女生: 67.2 },
            //     { product: 'Walnut Brownie', 男生: 55.2, 女生: 67.1 }
            // ]
            source: salaryData.map(item => {
                return {
                    product: item.label,
                    男生: item.b_count,
                    女生: item.g_count
                }
            })
        },
        series: [
            {
                type: 'pie',
                radius: '20%',
                center: ['50%', '30%'],
                radius: ['20%', '30%'],
            },
            {
                type: 'pie',
                radius: '20%',
                center: ['50%', '70%'],
                radius: ['20%', '30%'],
                encode: {
                    itemName: 'product',
                    value: '女生'
                }
            }
        ],
        color: ['#ffa11e', '#4e96fc', '#39bdfb', '#33d29a']
    }
    myBox.setOption(option)
}

//籍贯分布
function setorigin(provinceData) {
    let myBox = echarts.init(document.querySelector('#map'))
    const dataList = [
        { name: '南海诸岛', value: 0 },
        { name: '北京', value: 0 },
        { name: '天津', value: 0 },
        { name: '上海', value: 0 },
        { name: '重庆', value: 0 },
        { name: '河北', value: 0 },
        { name: '河南', value: 0 },
        { name: '云南', value: 0 },
        { name: '辽宁', value: 0 },
        { name: '黑龙江', value: 0 },
        { name: '湖南', value: 0 },
        { name: '安徽', value: 0 },
        { name: '山东', value: 0 },
        { name: '新疆', value: 0 },
        { name: '江苏', value: 0 },
        { name: '浙江', value: 0 },
        { name: '江西', value: 0 },
        { name: '湖北', value: 0 },
        { name: '广西', value: 0 },
        { name: '甘肃', value: 0 },
        { name: '山西', value: 0 },
        { name: '内蒙古', value: 0 },
        { name: '陕西', value: 0 },
        { name: '吉林', value: 0 },
        { name: '福建', value: 0 },
        { name: '贵州', value: 0 },
        { name: '广东', value: 0 },
        { name: '青海', value: 0 },
        { name: '西藏', value: 0 },
        { name: '四川', value: 0 },
        { name: '宁夏', value: 0 },
        { name: '海南', value: 0 },
        { name: '台湾', value: 0 },
        { name: '香港', value: 0 },
        { name: '澳门', value: 0 },
    ]
    dataList.forEach(item => {
        const res = provinceData.find(v => {
            return v.name.includes(item.name)
        })
        // console.log(res)
        // 数据赋值
        if (res !== undefined) {
            item.value = res.value
        }
    })
    // console.log(dataList)
    option = {
        title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} 位学员',
            borderColor: 'transparent',
            backgroundColor: 'rgba(0,0,0,0.5)',
            textStyle: {
                color: '#fff',
            },
        },
        visualMap: {
            min: 0,
            max: 6,
            left: 'left',
            bottom: '20',
            text: ['6', '0'],
            inRange: {
                color: ['#ffffff', '#0075F0'],
            },
            show: true,
            left: 40,
        },
        geo: {
            map: 'china',
            roam: false,
            zoom: 1.0,
            label: {
                normal: {
                    show: true,
                    fontSize: '10',
                    color: 'rgba(0,0,0,0.7)',
                },
            },
            itemStyle: {
                normal: {
                    borderColor: 'rgba(0, 0, 0, 0.2)',
                    color: '#e0ffff',
                },
                emphasis: {
                    areaColor: '#34D39A',
                    shadowOffsetX: 0,
                    shadowOffsetY: 0,
                    shadowBlur: 20,
                    borderWidth: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)',
                },
            },
        },
        series: [
            {
                name: '籍贯分布',
                type: 'map',
                geoIndex: 0,
                data: dataList,
            },
        ],
    }
    myBox.setOption(option)
}
